package com.leps.nq.bu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leps.nq.bi.vo.SimpleEntry;
import com.leps.nq.bu.entity.BuPlc;
import com.leps.nq.bu.entity.BuPump;
import com.leps.nq.bu.service.IBuPlcService;
import com.leps.nq.bu.service.IBuPumpService;
import com.leps.nq.bu.vo.PumpQueryModel;
import com.leps.nq.bu.vo.PumpVo;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.op.service.IOpWaterTestService;
import com.leps.nq.tx.core.PlcReadWriteService;
import com.leps.nq.tx.service.IReadHistoryService;
import com.leps.nq.tx.vo.PumpDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 泵表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/bu/bu-pump")
@Api(tags = "泵管理")
public class BuPumpController {
    @Autowired
    private IBuPumpService pumpService;
    @Autowired
    private IBuPlcService plcService;
    @Autowired
    private IReadHistoryService readHistoryService;
    @Autowired
    private PlcReadWriteService plcReadWriteService;
    @Autowired
    private IOpWaterTestService waterTestService;

    @GetMapping("/forSelect")
    @ApiOperation("泵下拉列表")
    public R<List<BuPump>> listForSelect(Integer plcId) {
        List<BuPump> list = this.pumpService.lambdaQuery().eq(plcId != null, BuPump::getPlcId, plcId).list();
        return RBuilder.ok(list);
    }

    @GetMapping
    @ApiOperation("泵列表")
    public R<PageVo<PumpVo>> list(PumpQueryModel queryModel) {
        PageVo<PumpVo> pageVo = this.pumpService.pageByQueryModel(queryModel);
        return RBuilder.ok(pageVo);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询泵信息")
    public R<BuPump> getById(@PathVariable Integer id) {
        return RBuilder.ok(this.pumpService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加泵")
    @Transactional
    @Log(title = "泵", businessType = BusinessType.INSERT)
    public R<?> save(@RequestBody BuPump pump) {
        boolean a = this.pumpService.save(pump);
        // 如果相应plc有多个泵，则修改为“撬装“
        long pumpNumberOfPlc = this.pumpService.count(new QueryWrapper<BuPump>().eq("plc_id", pump.getPlcId()));
        if (pumpNumberOfPlc > 1) {
            boolean b = this.plcService.lambdaUpdate().set(BuPlc::getPlcType2, "撬装").eq(BuPlc::getPlcId, pump.getPlcId()).update();
        }
        return RBuilder.ok(pump);
    }

    @PutMapping
    @ApiOperation("更新泵")
    @Log(title = "泵", businessType = BusinessType.UPDATE)
    public R<?> update(@RequestBody BuPump pump) {
        boolean b = this.pumpService.updateById(pump);
        return RBuilder.ofBoolean(b);
    }

    @DeleteMapping
    @ApiOperation("删除泵")
    @Log(title = "泵", businessType = BusinessType.DELETE)
    public R<?> remove(@RequestBody List<Integer> pumpIds) {
        boolean b = this.pumpService.removeBatchByIds(pumpIds);
        return RBuilder.ofBoolean(b);
    }

    @GetMapping("/latest-state")
    @ApiOperation("获取泵的最新状态(最近一次从PLC读取的泵数据)")
    public R<PumpDataVo> latestPumpState(int pumpId) {
        // fetch immediately
        this.plcReadWriteService.fetchOnePlc(pumpId);
        PumpDataVo vo = this.readHistoryService.getLastPumpStateByPumpId(pumpId);
        return RBuilder.ok(vo);
    }

    @GetMapping("/liquid-curve/{pumpId}")
    @ApiOperation("泵液位曲线")
    public R<List<SimpleEntry<Double>>> liquidCurve(@PathVariable int pumpId) {
        List<SimpleEntry<Double>> list = this.readHistoryService.getRecentLiquidLevels(pumpId);
        return RBuilder.ok(list);
    }

    @GetMapping("/iron-curve/{pumpId}")
    @ApiOperation("泵总铁曲线")
    public R<List<SimpleEntry<Double>>> ironCurve(@PathVariable int pumpId) {
        List<SimpleEntry<Double>> list = this.waterTestService.getRecentIrons(pumpId);
        return RBuilder.ok(list);
    }

    @GetMapping("/colony-curve/{pumpId}")
    @ApiOperation("泵细菌曲线")
    public R<List<SimpleEntry<Double>>> colonyCurve(@PathVariable int pumpId) {
        List<SimpleEntry<Double>> list = this.waterTestService.getRecentColonys(pumpId);
        return RBuilder.ok(list);
    }
}
